﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.Windows.Controls.Ribbon;

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class ProductListForm : ScrumMagic.CommonForm
    {
        // test ribbon window
        private ProductListRibbon productListRibbon;

        public override RibbonTab RibbonContextualTab
        {
          get 
          {
            productListRibbon.ribbonMenu.Tabs.Remove(productListRibbon.tabEdit);
            return productListRibbon.tabEdit;
          }
        }

        public ProductListForm()
        {
            InitializeComponent();

            productListRibbon = new ProductListRibbon();
            productListRibbon.FindButtonClicked += new EventHandler<TextEventArgs>(OnMenuFindButtonClicked);
            productListRibbon.tabEdit.Label = "Product List";
            

            try
            {
                SqlCommand com = DBController.MainConnection.CreateCommand();
                com.CommandText =
                    "select Product.Id, Product.Name, [Version].Name AS Version, Project.Name AS Project, [Status].Name AS Status, Project.Comment " + 
                    "from Product left join [Version] on Product.Version_Id = [Version].Id " + 
                    "inner join Project on Product.Project_Id = Project.Id " +
                    "inner join Status on Product.Status_Id = Status.Id " +
                    "where Product.id in (select product_Id from RoleInProduct where User_Id = @userId);";
                com.CommandType = System.Data.CommandType.Text;
                com.Parameters.AddWithValue("@userId", CurrentUser.Instance.Id);
                DataTable table = DBController.ExecuteDataTable(com);
                dataGrid1.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't get table Product",
                  "ScrumMagic",
                  MessageBoxButton.OK);
            }

        }

        void OnMenuFindButtonClicked(object sender, TextEventArgs e)
        {
            DataView dataSourse = dataGrid1.ItemsSource as DataView;
            dataSourse.RowFilter = string.Format("Name LIKE '{0}%'", e.Text);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (dataGrid1.SelectedItem == null)
                return;

            DataRowView row = (DataRowView)dataGrid1.SelectedItem;
            ProductCardForm card = new ProductCardForm((int)row["Id"]);
            card.Title = "Product Card";
            FormController.OpenForm(card, FormController.ContainerTypes.window);
            //card.Show();
            

        }

        private void CommonForm_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void dataGrid1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("CurrentCellchanged!!!");
        }

        private void dataGrid1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
//            MessageBox.Show("DataContextchanged!!!");
        }

        private void dataGrid1_SelectedCellsChanged(object sender, Microsoft.Windows.Controls.SelectedCellsChangedEventArgs e)
        {
            
            //MessageBox.Show("SelectedCellsChanged!!!");
        }

        private void dataGrid1_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
        {
            //MessageBox.Show(rowSt);
 
            //MessageBox.Show(dataGrid1.SelectedItem.GetType().ToString());

            //MessageBox.Show("CellEditEnding!!!");
        }

        //private void Item_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    ListViewItem item = ((ListViewItem)(sender));
        //    this.ListView1.SelectedItem = item.DataContext;
        //}
     

    }
}
